#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace GameFramework.ScreenSystem
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class OptionsMenuScreen : MenuScreen
    {
        #region Fields

        enum Ungulate
        {
            BactrianCamel,
            Dromedary,
            Llama,
        }

        static Ungulate currentUngulate = Ungulate.Dromedary;

        static string[] languages = { "C#", "French", "Deoxyribonucleic acid" };
        static int currentLanguage = 0;

        static bool frobnicate = true;

        static int elf = 23;

        #endregion

        #region Initialization


        /// <summary>
        /// Constructor populates the menu with empty strings: the real values
        /// are filled in by the Update method to reflect the changing settings.
        /// </summary>
        public OptionsMenuScreen(string musicToPlay,MenuTransition Transition):base(musicToPlay,Transition)
        {
            MenuEntries.Add(string.Empty);
            MenuEntries.Add(string.Empty);
            MenuEntries.Add(string.Empty);
            MenuEntries.Add(string.Empty);
            MenuEntries.Add(string.Empty);
            MenuEntries.Add(string.Empty);
            //MenuEntries.Add(string.Empty);
            MenuEntries.Add("Back");

            GameManager.AudioManager.StopAllPlaying();

            if(!String.IsNullOrEmpty(musicToPlay))
                GameManager.AudioManager.Play(musicToPlay);
        }


        #endregion

        #region Handle Input


        /// <summary>
        /// Updates the options screen, filling in the latest values for the menu text.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            MenuEntries[0] = "Music Volume: " + (GameManager.StorageManager.GameOptions.MusicEnabled ? "On" : "Off");
            MenuEntries[1] = "SFX Volume: " + (GameManager.StorageManager.GameOptions.SfxEnabled ? "On" : "Off");
            MenuEntries[2] = "Preferred ungulate: " + currentUngulate.ToString();
            MenuEntries[3] = "Language: " + languages[currentLanguage];
            MenuEntries[4] = "Frobnicate: " + (frobnicate ? "on" : "off");
            MenuEntries[5] = "elf: " + elf.ToString();
        }


        /// <summary>
        /// Responds to user menu selections.
        /// </summary>
        protected override void OnSelectEntry(int entryIndex)
        {
            switch (entryIndex)
            {
                case 0:
                    // Tobble the current state of the volume status.
                    GameManager.StorageManager.GameOptions.MusicEnabled = !GameManager.StorageManager.GameOptions.MusicEnabled;
                    
                    // Using the XACT categorys and some good grouping it is possible to 
                    // easily toggle the volume of any playing music cues. NOTE that any XACT 
                    // audio cues that have not been associated with either the Music or UI 
                    // categories will not be effected by this muting process.
                    if (GameManager.StorageManager.GameOptions.MusicEnabled)
                        GameManager.AudioManager.Categories["Music"].Volume = GameFramework.AudioSystem.Volume.Full;
                    else
                        GameManager.AudioManager.Categories["Music"].Volume = GameFramework.AudioSystem.Volume.Mute;

                    break;

                case 1:
                    // Change the Sfx Volume On/Off setting.
                    GameManager.StorageManager.GameOptions.SfxEnabled = !GameManager.StorageManager.GameOptions.SfxEnabled;

                    // Using the XACT categorys and some good grouping it is possible to 
                    // easily toggle the volume of any playing sound effect cues. NOTE that 
                    // any XACT audio cues that have not been associated with either the Music 
                    // or UI categories will not be effected by this muting process.
                    if (GameManager.StorageManager.GameOptions.SfxEnabled)
                        GameManager.AudioManager.Categories["UI"].Volume = GameFramework.AudioSystem.Volume.Full;
                    else
                        GameManager.AudioManager.Categories["UI"].Volume = GameFramework.AudioSystem.Volume.Mute;

                    break;

                case 2:
                    // Change the "preferred ungulate" setting.
                    currentUngulate++;

                    if (currentUngulate > Ungulate.Llama)
                        currentUngulate = 0;

                    break;

                case 3:
                    // Change the "language" setting.
                    currentLanguage = (currentLanguage + 1) % languages.Length;
                    break;

                case 4:
                    // Change the "frobnicate" setting.
                    frobnicate = !frobnicate;
                    break;

                case 5:
                    // Change the "elf" setting.
                    elf++;
                    break;

                case 6:
                    // Go back to the main menu.

                    // Stop all currently playing sounds.
                    GameManager.AudioManager.StopAllPlaying();

                    GameManager.StorageManager.SaveGameOptions();

                    ExitScreen();
                    
                    break;
            }
        }


        /// <summary>
        /// When the user cancels the options screen, go back to the main menu.
        /// </summary>
        protected override void OnCancel()
        {
            // Stop all currently playing sounds.
            GameManager.AudioManager.StopAllPlaying();

            ExitScreen();
        }


        #endregion
    }
}
